import { NextRequest, NextResponse } from "next/server";

export const config = {
  // matcher 中的路由才会执行 middleware
  // 格式 字符串、数组、正则
  matcher: [
    // "/test", "/userList/:path*",
    // 特殊写法：中间件处理除以下路由外的所有路由 api、_next/static、_next/image、favicon.ico
    "/((?!api|_next/static|_next/image|favicon.ico).*)",
  ],
};

export const middleware = (request: NextRequest) => {
  console.log(request.nextUrl.pathname, "middleware run");
  if (request.nextUrl.pathname.startsWith("/about")) {
    return NextResponse.rewrite(new URL("/test", request.url));
  }
  if (request.nextUrl.pathname.startsWith("/think")) {
    return NextResponse.redirect(new URL("/test", request.url));
  }
};
